package Product;

public class Storage {
	private Product[] products = new Product[10];
	private int top = 0;
	
	//放入
	public synchronized void push(Product p){
		while(top == products.length){
			try{
				wait();
			}catch(InterruptedException e){
				e.printStackTrace();
			}
			//把产品放入仓库
			products[top++]=p;
			System.out.println(Thread.currentThread().getName()+"生产了产品"+p);
			notifyAll();//唤醒等待的线程
		}
	}
	
	
	//取出
	public synchronized Product pop(){
		while(top==0){
			try{
				wait();
			}catch(InterruptedException e){
				e.printStackTrace();
			}

		}
		
		//取产品
		--top;
		Product p = new Product(products[top].getId(),products[top].getName());
		products[top]=null;
		System.out.println(Thread.currentThread().getName()+"消费了产品"+p);
		notifyAll();//唤醒等待线程
		return p;
	}
}
